<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use App\Services\ConfigService;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use App\Http\Controllers\Controller;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class SiteController extends Controller
{

    private $model;

    private $configs;

    private $form;

    protected $tabs = [
        [
            'text'=>'網站設置',
            'method'=>'site',
        ],
        [
            'text'=>'Google Search',
            'method'=>'google',
        ],

    ];



    public function __construct(\App\Models\Config $config, \Dcat\Admin\Widgets\Form $form)
    {
        $this->model = $config;
        $this->configs = $this->model->pluck('content','name')->toArray();

        $this->form = $form;
        $form->action(admin_url('site'));
        $form->disableResetButton();
    }

    public function index(Content $content){

        $content->row(function (Row $row) {
            $type = request('_t', 'site');
            $tab = new Tab();

            foreach($this->tabs as $item){
                if($item['method'] == $type){
                    $tab->add($item['text'], $this->$type(),true);
                }else{
                    $tab->addLink($item['text'], request()->fullUrlWithQuery(['_t' => $item['method']]));
                }
            }



            $row->column(12, $tab->withCard());
        });

        return $content
            ->header('基本設置');

    }

    public function store(Request $request){

        foreach($request->config as $key=>$item){

            if ($key == 'delivery_type' || is_array($item)){

                foreach ($item as $k=>&$val){
                    if(isset($val['_remove_']) && $val['_remove_'] == 1){
                        unset($item[$k]);
                    }
                }
                $item = json_encode($item);
            }




            $this->model->updateOrCreate(
                ['name'=>$key],
                [
                    'name'=>$key,
                    'content'=>$item,
                ]
            );
        }
        ConfigService::cache();
        $form = $this->form;
        return $form->response()->success('保存成功')->refresh()->send();
    }

    protected function site(){

        $form = $this->form;

        $form->image('config.logo', 'LOGO')->url('upload/files')->autoUpload()->default(Arr::get($this->configs,'logo',''));

        $form->image('config.favicon', 'favicon')->url('upload/files')->autoUpload()->default(Arr::get($this->configs,'favicon',''));
        $form->image('config.loading_image', 'Loading')->url('upload/files')->autoUpload()->default(Arr::get($this->configs,'loading_image',''));


        $form->text('config.copyright', '版版所有')->default(Arr::get($this->configs,'copyright',''));


        return $form;
    }

    protected function google(){
        $form = $this->form;
        $form->textarea('config.robots', 'robots')->rows(10)->required()->default(Arr::get($this->configs,'robots',''));
        if(env('APP_M_URL')) {
            $form->textarea('config.m_robots', 'robots (M站)')->rows(10)->required()->default(Arr::get($this->configs, 'm_robots', ''));
        }
        $form->radio('config.google_verify_type','谷歌验证方式')
            ->when(1, function ( $form) {
                $form->text('config.google_verify_code','HTML标记验证')->placeholder('如：<meta name="google-site-verification" content="vTiPleMXy8BQ8KMxv_ynj8OpsuLqOGdT-VASFhj123" />')->default(Arr::get($this->configs,'google_verify_code',''));
            })
            ->when(2, function ( $form) {
                $form->file('config.google_verify_file', '谷歌验证文件')->autoUpload()->url('upload/files/google_verify')->default(Arr::get($this->configs,'google_verify_file',''));
            })->options([
                1 => 'HTML标记',
                2 => '文件',

            ])->default(Arr::get($this->configs,'google_verify_type',1));


        $form->textarea('config.google_ga', 'Google Analytics')->rows(10)->default(Arr::get($this->configs,'google_ga',''));
        /*$form->text('config.seo_title_tail','TITLE自动尾部内容')->default(Arr::get($this->configs,'seo_title_tail',''));*/
        $form->radio('config.close_googlebot','谷歌蜘蛛訪問')->options(['1' => '禁止', '0'=> '开放'])->default(Arr::get($this->configs,'close_googlebot','0'));
        $form->textarea('config.googlebot_index_page','谷歌首页内容')->rows(20)->default(Arr::get($this->configs,'googlebot_index_page'));
        return $form;
    }




}
